package com.xm.settlement.service.impl;

import com.baomidou.framework.service.impl.SuperServiceImpl;
import com.baomidou.mybatisplus.plugins.Page;
import com.xm.settlement.commons.base.ApiResult;
import com.xm.settlement.commons.utils.PageInfo;
import com.xm.settlement.mapper.ConsumptionUploadMapper;
import com.xm.settlement.mapper.UserExpansionMapper;
import com.xm.settlement.model.AppPackageDesc;
import com.xm.settlement.model.ConsumptionUpload;
import com.xm.settlement.model.ScorePool;
import com.xm.settlement.model.UserExpansion;
import com.xm.settlement.service.ConsumptionUploadService;
import com.xm.settlement.service.UserExpansionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Administrator on 2017/4/13.
 */
@Service
public class ConsumptionUploadServiceImpl extends SuperServiceImpl<ConsumptionUploadMapper,ConsumptionUpload> implements ConsumptionUploadService {

    @Autowired
    private ConsumptionUploadMapper consumptionUploadMapper;

    @Autowired
    private UserExpansionService userExpansionService;

    @Override
    public void selectDataGrid(PageInfo pageInfo) {
        Page<ConsumptionUpload> page = new Page<ConsumptionUpload>(pageInfo.getNowpage(), pageInfo.getSize());
        List<ConsumptionUpload> list = consumptionUploadMapper.selectConsumptionUploadList(page);
        pageInfo.setRows(list);
        pageInfo.setTotal(page.getTotal());
    }

    /***
     *
     * 1.审核通过  2.未通过
     */
    @Override
    public Integer passVerify(Long id,Integer type) {
        try {
         /*   ApiResult apiResult = new ApiResult();*/
            ConsumptionUpload consumptionUpload = consumptionUploadMapper.selectById(id);
            if(type == 1){
                consumptionUpload.setStatus(1);
                consumptionUploadMapper.updateSelectiveById(consumptionUpload);

                /**----------------------审核通过增加增值点----------------------*/
                Double amount = consumptionUpload.getAmount();
                UserExpansion userExpansion = new UserExpansion();
                userExpansion.setLogin_name(consumptionUpload.getConsumption_name());
                userExpansion = userExpansionService.selectOne(userExpansion);
                int i =  consumptionUpload.getAmount().intValue()/1000  ;
                userExpansion.setIncrement(userExpansion.getIncrement() + i);
                userExpansionService.updateSelectiveById(userExpansion);
                return 1;
            }else {
                consumptionUpload.setStatus(2);
                consumptionUploadMapper.updateSelectiveById(consumptionUpload);
                return 2;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
